package com.link2room.ironhide.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.PinyinUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.enums.exception.IronhideSysExceptionEnum;
import com.link2room.ironhide.dto.param.manager.L2RGroupAreaManagerParamDto;
import com.link2room.ironhide.entity.L2RGroupArea;
import com.link2room.ironhide.entity.L2RHotelBaseinfo;
import com.link2room.ironhide.exception.IronhideException;
import com.link2room.ironhide.dto.result.base.L2RGroupAreaChildrenDto;
import com.link2room.ironhide.dto.result.base.L2RGroupAreaDto;
import com.link2room.ironhide.facade.IGroupAreaManagerFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by sss on 2016/10/11.
 */
@Component("groupAreaManagerFacadeService")
public class GroupAreaManagerFacadeServiceImpl implements IGroupAreaManagerFacadeService {
    @Resource
    private ICommonService commonService;


    /**
     * 更新集团城市
     * @param l2RGroupAreaManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RGroupAreaDto updateL2RGroupArea(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto) throws Exception {
        L2RGroupArea l2rGroupArea =commonService.findOneById(L2RGroupArea.class,l2RGroupAreaManagerParamDto.getL2RGroupAreaDto().getId());
        if(StringUtil.isNotBlank(l2RGroupAreaManagerParamDto.getL2RGroupAreaDto().getIsHot())){
            l2rGroupArea.setIsHot(l2RGroupAreaManagerParamDto.getL2RGroupAreaDto().getIsHot());
        }
        if(StringUtil.isNotBlank(l2RGroupAreaManagerParamDto.getL2RGroupAreaDto().getIsHalt())){
            l2rGroupArea.setIsHalt(l2RGroupAreaManagerParamDto.getL2RGroupAreaDto().getIsHalt());
        }
        commonService.merge(l2rGroupArea);
        return ClassConverUtil.copyPropertiesToDto(l2rGroupArea, L2RGroupAreaDto.class, false);
    }

    /**
     * 集团城市列表
     * @param l2RGroupAreaManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<L2RGroupAreaDto> listL2RGroupArea(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto) throws Exception {
        L2RGroupArea queryga=new L2RGroupArea();
        queryga.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        HashMap<String,String> order=new HashMap<>();
        QueryResult<L2RGroupArea> l2RGroupAreaResult=commonService.findAllEQ(L2RGroupArea.class,queryga,l2RGroupAreaManagerParamDto.getCurrentPage(),l2RGroupAreaManagerParamDto.getPageSize(),order,null);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2RGroupAreaResult,L2RGroupAreaDto.class, false);
    }

    /**
     * 列出级联城市列表
     * @param l2RGroupAreaManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public List<L2RGroupAreaChildrenDto> listL2RGroupAreaChildren(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto) throws Exception {
        L2RGroupArea queryga=new L2RGroupArea();
        queryga.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        queryga.setIsHalt(DefaultValue.F);
        HashMap<String,String> order=new HashMap<>();
        order.put(L2RGroupArea.L2RGroupArea_.isHot.toString(), CriteriaUtil.DESC);
        String[] groupByCountry=new String[]{L2RGroupArea.L2RGroupArea_.countryCode.toString()};
        String[] groupByProvince=new String[]{L2RGroupArea.L2RGroupArea_.provinceCode.toString()};

        QueryResult<L2RGroupArea> l2RGroupAreaCountryResult=commonService.findAllEQ(L2RGroupArea.class,queryga,null,null,order,groupByCountry);
        List<L2RGroupArea> countryList = l2RGroupAreaCountryResult.getResultlist();
        QueryResult<L2RGroupArea> l2RGroupAreaProvinceResult=commonService.findAllEQ(L2RGroupArea.class,queryga,null,null,order,groupByProvince);
        List<L2RGroupArea> provinceList = l2RGroupAreaProvinceResult.getResultlist();
        QueryResult<L2RGroupArea> l2RGroupAreaCityResult=commonService.findAllEQ(L2RGroupArea.class,queryga,null,null,order,null);
        List<L2RGroupArea> cityList = l2RGroupAreaCityResult.getResultlist();
        List<L2RGroupAreaChildrenDto> countryChildren=new ArrayList<>();
        if(countryList!=null&&countryList.size()>0){
            for (L2RGroupArea country: countryList) {
                List<L2RGroupAreaChildrenDto> provinceChildren=new ArrayList<>();
                if (provinceList!=null&&provinceList.size()>0){
                    for (L2RGroupArea province:provinceList){
                        if(country.getCountryCode().equals(province.getCountryCode())){
                            List<L2RGroupAreaChildrenDto> cityChildren=new ArrayList<>();
                            if (cityList!=null&&cityList.size()>0){
                                for(L2RGroupArea city:cityList){
                                    if(province.getProvinceCode().equals(city.getProvinceCode())){
                                        L2RGroupAreaChildrenDto l2RGroupAreaCity=new L2RGroupAreaChildrenDto();
                                        l2RGroupAreaCity.setLabel(city.getCityName());
                                        l2RGroupAreaCity.setValue(city.getCityCode().toString());
                                        cityChildren.add(l2RGroupAreaCity);
                                    }
                                }
                            }
                            L2RGroupAreaChildrenDto l2RGroupAreaProvince=new L2RGroupAreaChildrenDto();
                            l2RGroupAreaProvince.setLabel(province.getProvinceName());
                            l2RGroupAreaProvince.setValue(province.getProvinceCode().toString());
                            l2RGroupAreaProvince.setChildren(cityChildren);
                            provinceChildren.add(l2RGroupAreaProvince);
                        }
                    }
                }
                L2RGroupAreaChildrenDto l2RGroupAreaCountry=new L2RGroupAreaChildrenDto();
                l2RGroupAreaCountry.setLabel(country.getCountryName());
                l2RGroupAreaCountry.setValue(country.getCountryCode());
                l2RGroupAreaCountry.setChildren(provinceChildren);
                countryChildren.add(l2RGroupAreaCountry);
            }
        }
        return countryChildren;
    }

    @Override
    public void updateL2RGroupAreaForHotelNum(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto)throws Exception{
        L2RHotelBaseinfo queryhb=new L2RHotelBaseinfo();
        queryhb.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        queryhb.setCity(l2RGroupAreaManagerParamDto.getCityCode());
        long num = commonService.getCountAllEQ(L2RHotelBaseinfo.class, queryhb, null);
        L2RGroupArea queryga=new L2RGroupArea();
        queryga.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        queryga.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
        L2RGroupArea l2rGroupArea = commonService.findOneEQ(L2RGroupArea.class, queryga);
        if(l2rGroupArea!=null){
            l2rGroupArea.setHotelNum(Integer.parseInt(String.valueOf(num)));
            commonService.merge(l2rGroupArea);
        }
    }

    /**
     * 保存集团城市
     * @param l2RGroupAreaManagerParamDto
     * @throws Exception
     */
    @Override
    public L2RGroupAreaDto saveL2RGroupArea(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto)throws Exception{
        L2RGroupAreaDto l2rGroupAreaDto = l2RGroupAreaManagerParamDto.getL2RGroupAreaDto();
        l2rGroupAreaDto.setCountrySearchCode(PinyinUtil.getStringPinYin(l2rGroupAreaDto.getCountryName()));
        l2rGroupAreaDto.setProvinceSearchCode(PinyinUtil.getStringPinYin(l2rGroupAreaDto.getProvinceName()));
        l2rGroupAreaDto.setCitySearchCode(PinyinUtil.getStringPinYin(l2rGroupAreaDto.getCityName()));
        L2RGroupArea queryGA=new L2RGroupArea();
        queryGA.setLhotelGroupCode(l2rGroupAreaDto.getLhotelGroupCode());
        queryGA.setCityCode(l2rGroupAreaDto.getCityCode());
        QueryResult<L2RGroupArea> groupAreaResult = commonService.findAllEQ(L2RGroupArea.class, queryGA);
        if(groupAreaResult.getResultlist()==null||groupAreaResult.getResultlist().size()<=0){
            L2RGroupArea l2rGroupArea = ClassConverUtil.copyProperties(l2rGroupAreaDto, L2RGroupArea.class);
            commonService.save(l2rGroupArea);
            return ClassConverUtil.copyPropertiesToDto(l2rGroupArea,L2RGroupAreaDto.class, false);
        }else {
            throw new IronhideException(IronhideSysExceptionEnum.GROUP_AREA_EXIT);
        }
    }




}
